<template>
    <el-dialog 
        v-model="dialogVisible" 
        title="验证码" 
        width="420px"
        :close-on-click-modal="false"
        @close="handleClose"
        center
    >
        <div class="code-container">
            <!-- 成功提示 -->
            <div class="success-info">
                <el-icon :size="48" color="#67C23A">
                    <SuccessFilled />
                </el-icon>
                <h3 class="success-text">验证码已生成</h3>
            </div>
            
            <!-- 验证码显示区域 -->
            <div class="code-display">
                <div class="code-label">您的验证码是：</div>
                <div class="code-number">{{ verificationCode }}</div>
                <el-button 
                    type="primary" 
                    size="small" 
                    @click="copyCode"
                    style="margin-top: 15px;"
                >
                    <el-icon style="margin-right: 5px;"><CopyDocument /></el-icon>
                    复制验证码
                </el-button>
            </div>
            
            <!-- 提示信息 -->
            <div class="tips">
                <el-alert 
                    type="warning" 
                    :closable="false"
                    show-icon
                >
                    <template #default>
                        <p style="margin: 0; font-size: 13px;">
                            <strong>开发模式：</strong>这是模拟的验证码，可直接复制使用。<br>
                            实际生产环境中，验证码将通过短信发送到手机。
                        </p>
                    </template>
                </el-alert>
            </div>
            
            <!-- 手机号显示 -->
            <div class="phone-display" v-if="phone">
                <el-text type="info" size="small">
                    接收手机：{{ formatPhone(phone) }}
                </el-text>
            </div>
            
            <!-- 倒计时提示 -->
            <div class="expire-hint">
                <el-text type="info" size="small">
                    <el-icon><Clock /></el-icon>
                    验证码有效期5分钟
                </el-text>
            </div>
        </div>
        
        <template #footer>
            <el-button @click="handleClose">关闭</el-button>
            <el-button type="primary" @click="handleAutoFill">
                自动填充验证码
            </el-button>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { SuccessFilled, CopyDocument, Clock } from '@element-plus/icons-vue'

// Props定义
const props = defineProps<{
    visible: boolean
    phone?: string
    code?: string
}>()

// Emits定义
const emit = defineEmits<{
    'update:visible': [value: boolean]
    'confirm': [code: string]
}>()

// 验证码
const verificationCode = ref('')

// 生成6位随机验证码
const generateCode = () => {
    return Math.floor(100000 + Math.random() * 900000).toString()
}

// 监听visible变化，每次打开时生成新验证码
watch(() => props.visible, (newVal) => {
    if (newVal) {
        // 如果传入了code则使用传入的，否则生成新的
        verificationCode.value = props.code || generateCode()
        console.log('生成的验证码：', verificationCode.value)
    }
})

// 计算属性：控制对话框显示
const dialogVisible = computed({
    get: () => props.visible,
    set: (value) => emit('update:visible', value)
})

// 格式化手机号（中间4位显示为*）
const formatPhone = (phone: string) => {
    if (!phone || phone.length !== 11) return phone
    return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}

// 复制验证码
const copyCode = async () => {
    try {
        await navigator.clipboard.writeText(verificationCode.value)
        ElMessage.success('验证码已复制到剪贴板')
    } catch (err) {
        // 降级方案：使用传统方法复制
        const textarea = document.createElement('textarea')
        textarea.value = verificationCode.value
        textarea.style.position = 'fixed'
        textarea.style.opacity = '0'
        document.body.appendChild(textarea)
        textarea.select()
        try {
            document.execCommand('copy')
            ElMessage.success('验证码已复制到剪贴板')
        } catch (e) {
            ElMessage.error('复制失败，请手动复制')
        }
        document.body.removeChild(textarea)
    }
}

// 关闭对话框
const handleClose = () => {
    emit('update:visible', false)
}

// 自动填充验证码
const handleAutoFill = () => {
    emit('confirm', verificationCode.value)
    emit('update:visible', false)
    ElMessage.success('验证码已自动填充')
}
</script>

<style scoped>
.code-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px 20px 20px;
}

.success-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 25px;
    animation: fadeIn 0.5s ease-in;
}

.success-text {
    margin: 15px 0 0;
    font-size: 20px;
    color: #67C23A;
    font-weight: 600;
}

.code-display {
    width: 100%;
    text-align: center;
    padding: 25px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    margin-bottom: 20px;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.code-label {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.9);
    margin-bottom: 15px;
    font-weight: 500;
}

.code-number {
    font-size: 42px;
    font-weight: bold;
    letter-spacing: 8px;
    color: white;
    font-family: 'Courier New', monospace;
    padding: 15px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    user-select: all;
    cursor: pointer;
    transition: all 0.3s ease;
}

.code-number:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: scale(1.02);
}

.tips {
    width: 100%;
    margin-bottom: 15px;
}

.phone-display {
    text-align: center;
    padding: 12px;
    background: #F0F9FF;
    border-radius: 8px;
    width: 100%;
    margin-bottom: 10px;
}

.expire-hint {
    display: flex;
    align-items: center;
    gap: 5px;
    color: #909399;
    font-size: 13px;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

:deep(.el-button) {
    transition: all 0.3s ease;
}

:deep(.el-button:hover) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}
</style>

